 <template>
  <div class="page">
    <s-header :name="'审批办理'" :back="'/government/bulid-dept'"></s-header>
    <div class="card-box">
      <div class="card-item">
        <div class="card-header">
          <div class="header-title">建房申请单号：{{ buildDataForm.applyOrderNo }}</div>
        </div>
        <van-divider />
        <div class="card-body card-body-nop">
          <van-cell title="申请人" :value="buildDataForm.applyName" />
          <van-cell title="申请时间" :value="buildDataForm.applyTime" />
          <van-cell title="联系电话" :value="buildDataForm.applyPhone" />
          <van-cell title="申请理由" :value="buildDataForm.applyReasons" />
          <van-cell title="建房类型" :value="buildDataForm.buildTypeName" />
          <van-cell title="家庭住址" :value="buildDataForm.applyHouseAddress" />
        </div>
      </div>

      <div class="card-item">
        <div class="card-header">
          <div class="item-head">
            <div class="title title-required">审查项</div>
            <div class="text-add" @click="handleToEdit(null, 'add')">新增</div>
          </div>
        </div>
        <van-divider />
        <div class="card-body">
          <div class="item-body">
            <van-cell
              v-for="item in reviewItems"
              :key="item.itemId"
              :title="item.itemName"
              is-link
              @click="handleToEdit(item.reviewItemId, 'edit')"
            />
          </div>
        </div>
      </div>

      <van-form ref="dataFormRef">
        <div class="card-item">
          <div class="card-header">
            <div class="header-title title-required">审批结果</div>
          </div>
          <van-divider />
          <div class="card-body">
            <van-field v-model="dataForm.presentPerson" class="noPadding person" label="到场人员" placeholder="请输入到场人员" />
            <van-radio-group v-model="dataForm.reviewResult" :rules="dataRule.reviewResult" direction="horizontal">
              <van-radio name="1">合格</van-radio>
              <van-radio name="2">不合格</van-radio>
            </van-radio-group>
          </div>
        </div>

        <div class="card-item">
          <div class="card-header">
            <div class="header-title title-required">办理意见</div>
          </div>
          <van-divider />
          <div class="card-body">
            <van-field
              v-model="dataForm.handleOpinions"
              :rules="dataRule.handleOpinions"
              class="noPadding"
              rows="3"
              autosize
              label=""
              type="textarea"
              placeholder="请输入办理意见"
            />
          </div>
        </div>
      </van-form>

      <div class="card-item last-item">
        <div class="card-header">
          <div class="item-head">
            <div class="title">附件上传</div>
          </div>
        </div>
        <van-divider />
        <div class="card-body">
          <image-upload v-model:value="otherFileList" @uploadBack="uploadBack"></image-upload>
        </div>
      </div>
    </div>

    <div class="btn-group">
      <van-button type="button" class="cus-btn btn-default" @click="handleSave">保存</van-button>
      <van-button :loading="isCommit" type="button" class="cus-btn btn-primary" @click="handleTransact">提交</van-button>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, ref, onMounted, getCurrentInstance, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { Toast } from 'vant'
import sHeader from '@/components/SimpleHeader'
import ImageUpload from '@/components/ImageUpload'
import { getBulidDeptDetail, saveBulidDept } from '@/service/govern'
export default {
  components: { sHeader, ImageUpload },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const store = useStore()
    const dataFormRef = ref(null)
    const state = reactive({
      isCommit: false,
      dataForm: {
        id: '',
        reviewResult: '1',
        handleOpinions: '',
        presentPerson: '',
      },
      buildDataForm: {
        applyOrderNo: '',
        applyName: '',
        applyTime: '',
        applyPhone: '',
        applyReasons: '',
        buildTypeName: '',
        applyHouseAddress: '',
      },
      reviewItems: [],
      otherFileList: [],
      attachInfoDTOList: [],
      dataRule: {
        handleOpinions: [{ required: true, message: '请输入办理意见', trigger: 'onBlur' }],
        reviewResult: [{ required: true, message: '请选择审批结果', trigger: 'onChange' }],
      },
    })

    // 全局属性
    const { listAssign } = getCurrentInstance().appContext.config.globalProperties
    store.dispatch('getFileServerPrefix')

    onMounted(async () => {
      const { id } = route.params
      if (id && id != 'undefined') {
        getBulidDeptDetail({ applyBaseId: id }).then((res) => {
          const data = res.data
          state.reviewItems = data.reviewList || []

          listAssign(state.buildDataForm, data)
          listAssign(state.dataForm, data)

          let otherFileList = data.otherFileList
          if (otherFileList && otherFileList.length > 0) {
            otherFileList.forEach((item) => {
              if (item.url.indexOf(fileServerPrefix.value) === -1) {
                item.filePath = item.url
                item.url = fileServerPrefix.value + item.url
              }
            })
          }
          // {
          //         url: fileServerPrefix.value + atta.url,
          //         name: atta.name,
          //         size: atta.size,
          //         suffix: atta.suffix,
          //         type: atta.type,
          //         filePath: atta.url,
          //       }
          state.otherFileList = otherFileList
          // state.attachInfoDTOList = [...data.otherFileList]
        })
      } else {
        router.push({ path: `/government/bulid-dept` })
      }
    })

    const handleToEdit = (id, type) => {
      sendRequest('0').then(() => {
        router.push({
          path: '/government/review-item',
          query: { id: id, type: type, pageId: route.params.id, reviewModule: '4', pagePath: '/government/bulid-dept/transact' },
        })
      })
    }

    // 文件上传后回调
    const uploadBack = (fileList) => {
      state.otherFileList = fileList
    }

    const handleSave = () => {
      sendRequest('0').then(() => {
        Toast.success('保存成功')
        setTimeout(() => {
          router.push({ path: `/government/bulid-dept` })
        }, 500)
      })
    }

    const handleTransact = () => {
      dataFormRef.value.validate().then(() => {
        if (state.reviewItems.length === 0) {
          Toast.fail('请新增审查项')
          return
        }
        sendRequest('1').then(() => {
          Toast.success('提交成功')
          setTimeout(() => {
            router.push({ path: `/government/bulid-dept` })
          }, 500)
        })
      })
    }

    const sendRequest = (isSubmit) => {
      return new Promise((resolve, reject) => {
        let attachInfoDTOList = state.otherFileList.map((imgItem, index) => {
          return {
            name: imgItem.name,
            size: imgItem.size,
            suffix: imgItem.suffix,
            type: imgItem.type,
            url: imgItem.filePath ? imgItem.filePath : imgItem.url,
            sort: index,
          }
        })
        state.isCommit = true
        let params = state.dataForm
        params.attachInfoDTOList = attachInfoDTOList
        params.applyBaseId = route.params.id
        params.type = isSubmit
        saveBulidDept(params)
          .then(() => {
            state.isCommit = false
            resolve(true)
          })
          .catch((err) => {
            state.isCommit = false
            reject(err)
          })
      })
    }

    const fileServerPrefix = computed(() => {
      return store.state.fileServerPrefix
    })

    return {
      dataFormRef,
      fileServerPrefix,
      ...toRefs(state),
      handleTransact,
      handleSave,
      handleToEdit,
      uploadBack,
    }
  },
}
</script>

<style lang="less" scoped>
@import '@/common/style/mixin';
.page {
  color: #2e3f6a;
  font-weight: 400;
  .card-box {
    margin-top: 10px;
    .van-divider {
      margin: 10px 0 0;
    }
    .card-header {
      font-size: 15px;
      .item-head {
        .fj();
        .text-add {
          font-weight: 400;
          color: #3487f6;
        }
        .text-desc {
          font-size: 13px;
          font-weight: 400;
          color: #9297ae;
        }
      }
      .title-required::before {
        margin-right: 5px;
        color: var(--van-field-required-mark-color);
        content: '*';
      }
    }
    .card-body {
      padding-bottom: 10px;
      .person {
        margin-bottom: 10px;
        padding-bottom: 10px;
      }
    }
    .last-item {
      margin-bottom: 0;
    }
  }
  .btn-group {
    padding: 30px 0 20px;
    .cus-btn {
      width: 167px;
    }
  }
}
</style>
